package com.softer.wxzj.controller;

import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.aop.annotation.LogRecord;
import com.softer.wxzj.common.aop.enums.LogOperationType;
import com.softer.wxzj.common.aop.enums.LogOperationUnit;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.entity.BaseModifyDetail;
import com.softer.wxzj.entity.BaseOrganization;
import com.softer.wxzj.entity.BaseRegionInfo;
import com.softer.wxzj.entity.FunBalInfo;
import com.softer.wxzj.service.BaseRegionInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:小区基本信息实现类
 * @ClassName: BaseRegionInfoController
 * @author: youmingke
 * @date: 2019-05-20 16:31
 */
@RestController
@RequestMapping("/baseRegionInfo")
@Slf4j
public class BaseRegionInfoController {
    @Autowired
    BaseRegionInfoService baseRegionInfoService;

    /**
     * 添加审核
     *
     * @param id      业务ID
     * @param opinion 审核意见
     * @param mark    审批通过 audit_pass
     *                审批否决 audit_veto
     *                审批退回 audit_back
     * @return 审核结果
     */
    @RequestMapping("/addAudit")
    @LogRecord(description = "小区添加审核", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult audit(@RequestParam("id") String id, String opinion, @RequestParam("mark") String mark) {
        return baseRegionInfoService.addAudit(id, opinion, mark);
    }

    /**
     * 新增提交审批
     *
     * @param id
     * @return
     */
    @RequestMapping("/submitAudit")
    @LogRecord(description = "小区提交审核", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult submitAudit(@RequestParam("id") String id) {
        return baseRegionInfoService.submitAudit(id);
    }

    /**
     * 变更审核
     *
     * @param id      变更Id
     * @param opinion 审核意见
     * @param mark    审批通过 audit_pass
     *                审批否决 audit_veto
     *                审批退回 audit_back
     * @return 审核结果
     */
    @RequestMapping("/modifyAudit")
    @LogRecord(description = "小区变更审批", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult modifyAudit(String id, String opinion,  String mark) {
        System.out.println("_________________________"+id);
        return baseRegionInfoService.modifyAudit(id, opinion, mark);
    }

    /**
     * 小区变更
     *
     * @param baseRegionInfo 小区信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/modify")
    @LogRecord(description = "小区变更", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult modify(@RequestBody BaseRegionInfo baseRegionInfo) throws Exception {
        return baseRegionInfoService.modify(baseRegionInfo);
    }

    /**
     * 根据id查询变更详情
     *
     * @param id 变更类型id
     * @return
     */
    @RequestMapping("/getModifyDetailList")
    @LogRecord(description = "根据id查询变更详情", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseModifyDetail> getModifyDetailList(Page page, String id) {
        return baseRegionInfoService.getModifyDetailList(page, id);
    }

    /**
     * 获取待审核信息列表
     *
     * @param page
     * @param baseRegionInfo
     * @return
     */
    @RequestMapping("/getAuditList")
    @LogRecord(description = "获取新增待审批列表", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseRegionInfo> getAuditList(Page page, BaseRegionInfo baseRegionInfo) {
        return baseRegionInfoService.getAuditList(page, baseRegionInfo);
    }

    /**
     * 模糊查询小区待审批列表
     *
     * @param page           分页实体
     * @param baseRegionInfo 查询实体
     * @return 待审核小区列表
     */
    @RequestMapping("/fuzzyAuditList")
    @LogRecord(description = "模糊查询小区待审批列表", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseRegionInfo> fuzzyAuditList(Page page, BaseRegionInfo baseRegionInfo) {
        return baseRegionInfoService.fuzzyAuditList(page, baseRegionInfo);
    }

    /**
     * 获取变更信息列表 flowState:1待审批   1,2,3,4查询所有
     *
     * @param page
     * @param baseRegionInfo
     * @return
     */
    @RequestMapping("/getModifyList")
    @LogRecord(description = "获取变更信息列表", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseRegionInfo> getModifyList(Page page, BaseRegionInfo baseRegionInfo) {
        return baseRegionInfoService.getModifyList(page, baseRegionInfo);
    }

    /**
     * 获取变更信息列表 flowState:1待审批   1,2,3,4查询所有
     *
     * @param page
     * @param baseRegionInfo
     * @return
     */
    @RequestMapping("/getModifyAuditList")
    @LogRecord(description = "获取变更待审批信息列表", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseRegionInfo> getModifyAuditList(Page page, BaseRegionInfo baseRegionInfo) {
        return baseRegionInfoService.getModifyAuditList(page, baseRegionInfo);
    }

    /**
     * 新增备案   审批通过
     *
     * @param id
     * @return
     */
    @RequestMapping("/auditPass")
    @LogRecord(description = "小区备案直接审批通过", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult updateAudit(@RequestParam("id") String id) {
        return baseRegionInfoService.auditPass(id);
    }

    /**
     * 小区基本信息新增
     *
     * @param baseRegionInfo 小区实体
     * @return 结果
     */
    @RequestMapping("/add")
    @LogRecord(description = "小区信息新增", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.INSERT)
    public CommonResult add(@RequestBody BaseRegionInfo baseRegionInfo) {
        return baseRegionInfoService.insert(baseRegionInfo);
    }

    /**
     * 验证小区名称是否重复
     *
     * @param baseRegionInfoName
     * @return
     * @Author ludexing
     */
    @RequestMapping("/checkBaseRegionInfoName")
    @LogRecord(description = "验证小区名称是否重复", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.INSERT)
    public CommonResult checkBaseRegionInfoName(@RequestParam("baseRegionInfoName") String baseRegionInfoName, String id) {
        return baseRegionInfoService.checkBaseRegionInfoName(baseRegionInfoName, id);
    }


    /**
     * 小区基本信息修改
     *
     * @param baseRegionInfo 小区实体
     * @return 结果
     */
    @RequestMapping("/update")
    @LogRecord(description = "小区信息修改", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult update(@RequestBody BaseRegionInfo baseRegionInfo) {
        return baseRegionInfoService.update(baseRegionInfo);
    }

    /**
     * 备案、撤销
     *
     * @param id
     * @param state
     * @return
     */
    @RequestMapping("/updateState")
    @LogRecord(description = "备案/撤销备案", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult updateState(@RequestParam("id") String id, @RequestParam("state") String state) {
        return baseRegionInfoService.updateStateById(id, state);
    }

    /**
     * 根据id逻辑删除小区基本信息，修改base_region_info表的state为2
     *
     * @param id 小区id
     * @return 结果
     */
    @RequestMapping("/delete")
    @LogRecord(description = "小区信息逻辑删除", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.DELETE)
    public CommonResult delete(String id) {
        return baseRegionInfoService.delete(id);
    }

    /**
     * 根据id获取小区基本信息详情
     *
     * @param id      小区id
     * @param arcType 图档大类
     * @return 小区信息
     */
    @RequestMapping("/getInfo")
    @LogRecord(description = "根据id获取小区信息详情", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public BaseRegionInfo getInfo(String id, String arcType) {
        return baseRegionInfoService.getInfoById(id, arcType);
    }

    /**
     * 获取小区基本信息列表(小区建档)
     *
     * @param page           分页
     * @param baseRegionInfo 小区实体
     * @return 小区信息
     */
    @RequestMapping("/getList")
    @LogRecord(description = "获取建档小区信息详情", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseRegionInfo> getList(Page page, BaseRegionInfo baseRegionInfo) {
        return baseRegionInfoService.getList(page, baseRegionInfo);
    }

    /**
     * 模糊查询建档小区信息列表
     *
     * @param page           分页实体
     * @param baseRegionInfo 模糊查询参数
     * @return 小区信息
     */
    @RequestMapping("/fuzzySearch")
    @LogRecord(description = "模糊查询建档小区信息列表", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseRegionInfo> fuzzySearch(Page page, BaseRegionInfo baseRegionInfo) {
        return baseRegionInfoService.fuzzySearch(page, baseRegionInfo);
    }

    /**
     * 模糊查询房屋合并/拆分变更列表;小区信息维护列表； 业主信息维护列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 小区信息
     */
    @RequestMapping("/fuzzyRegionSearch")
    @LogRecord(description = "模糊查询审核通过的小区列表", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseRegionInfo> fuzzyRegionSearch(Page page, String param) {
        return baseRegionInfoService.fuzzyRegionSearch(page, param);
    }

    @RequestMapping("/getOrgList")
    @LogRecord(description = "查询小区下物业企业或开发企业列表", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public List<BaseOrganization> getOrgList(@RequestParam("id") String id, @RequestParam("orgTypeCode") String orgTypeCode) {
        return baseRegionInfoService.getOrgList(id, orgTypeCode);
    }

    /**
     * 小区名称
     *
     * @return
     */
    @RequestMapping("/selectRegion")
    @LogRecord(description = "小区名称", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public List<BaseRegionInfo> selectRegion() {
        return baseRegionInfoService.selectRegion();
    }

    @RequestMapping("/getListToBatchRef")
    @LogRecord(description = "查询小区列表，用在批量退款", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseRegionInfo> getListToBatchRef(Page page, BaseRegionInfo baseRegionInfo) {
        log.info("批量退款查询小区列表,操作人:{}", ShiroUserUtil.getUserName());
        return baseRegionInfoService.getListToBatchRef(page, baseRegionInfo);
    }

    /**
     * 临时查询
     *
     * @param page
     * @param fuzzy
     * @return
     */
    @RequestMapping("/selectOwnerFunBal")
    public PageInfo<FunBalInfo> selectOwnerFunBal(Page page, String fuzzy) {
        return baseRegionInfoService.selectOwnerFunBal(page, fuzzy);
    }

    /**
     * 临时查询
     *
     * @param baseRegionInfo
     * @return
     */
    @RequestMapping("/selectByCommit")
    public PageInfo<BaseRegionInfo> selectByCommit(Page page, BaseRegionInfo baseRegionInfo) {
        return baseRegionInfoService.selectByCommit(page, baseRegionInfo);
    }

    /**
     * 临时查询
     *
     * @param zoneCode
     * @return
     */
    @RequestMapping("/selectCommitteByZoneCode")
    public List<BaseRegionInfo> selectCommitteByZoneCode(String zoneCode, String type) {
        return baseRegionInfoService.selectCommitteByZoneCode(zoneCode, type);
    }

    /**
     * 查询小区名称
     *
     * @param id 小区ID
     * @return 小区名称
     */
    @RequestMapping("/getRegionName")
    public String getRegionName(String id) {
        BaseRegionInfo info = getInfo(id, null);
        if (info != null) {
            return info.getRegionName();
        }
        return "";
    }
}
